//
//  SYStudentView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2020/3/28.
//  Copyright © 2020年 SanYi. All rights reserved.
//

#import "SYStudentView.h"

#import "SYTopLabView.h"
#import "SYInviteStudentTableViewCell.h"

@interface SYStudentView () <UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) SYTopLabView *topLabView;
@property (strong, nonatomic) SYBackTopView *backView;//顶部viwe
@property (strong, nonatomic) UITableView *curTableView;//
@property (strong, nonatomic) NSMutableArray *datalistArr;
@property (strong, nonatomic) UIButton *selectBtn;
@property (copy, nonatomic) NSString *classId;
@property (copy, nonatomic) NSString *secheduId;

@end



@implementation SYStudentView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        [self initSubView];
    }
    return self;
}
- (void)initSubView {
    [self addSubview:self.backView];
    [self.backView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(50);
    }];
    
    
    
    
    [self addSubview:self.topLabView];
    [self.topLabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.backView.mas_bottom);
        make.height.mas_equalTo(45);
    }];
    
    [self addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.topLabView.mas_bottom);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    _datalistArr = [NSMutableArray array];
    
    [_curTableView registerCellWithNIbNames:@[@"SYInviteStudentTableViewCell"]];
    
    
}

- (void)requestClassId:(NSString *)requestClassId scheduleId:(NSString *)scheduleId {
    self.classId = requestClassId;
    self.secheduId = scheduleId;
    [MBProgressHUD mj_showLoading];
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI fd_requestClassLessonNoAttendStudentListClassId:requestClassId scheduleId:scheduleId success:^(NSMutableArray *listArr) {
        [MBProgressHUD mj_hideHUD];
        [weakSelf.datalistArr removeAllObjects];
        [weakSelf.datalistArr addObjectsFromArray:listArr];
        [weakSelf.curTableView reloadData];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
        [MBProgressHUD zr_showError:error.domain];
    }];
}

#pragma mark - tab
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return 3;
        return self.datalistArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYInviteStudentTableViewCell *hourCell = [tableView dequeueReusableCellWithIdentifier:@"SYInviteStudentTableViewCell" forIndexPath:indexPath];
    
        NSDictionary *dict = self.datalistArr[indexPath.row];
        hourCell.kechengLab.text = [dict nilObjectForKey:@"courseName"];
        hourCell.keshiLab.text = [dict nilObjectForKey:@"lesson"];
        hourCell.xueshengLab.text = [dict nilObjectForKey:@"studentName"];
        hourCell.shangkeLab.text = [dict nilObjectForKey:@"beginDate"];
        hourCell.xuehaoLab.text = [dict nilObjectForKey:@"serial"];
    NSString *isConfirm = [dict nilObjectForKey:@"isInvitation"];
    if ([isConfirm isEqualToString:@"0"]) {
        hourCell.shifouyaoqingLab.text = @"未邀请";
         hourCell.yaoqingLab.text = @"邀请";
    } else {
        hourCell.shifouyaoqingLab.text = @"已邀请";
        hourCell.yaoqingLab.text = @"再次邀请";
    }

    hourCell.yaoqingshijianLab.text = [dict nilObjectForKey:@"inviteDate"];
    
    
    
    hourCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return hourCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dict = self.datalistArr[indexPath.row];
    [MBProgressHUD mj_showLoading];
    [SYRequestAPI fd_requestClassInviteStudentSudentIds:[dict nilObjectForKey:@"studentId"] classId:self.classId courseId:[dict nilObjectForKey:@"courseId"] scheduleiD:[dict nilObjectForKey:@"scheduleId"] lessonTimes:[dict nilObjectForKey:@"lessonTimes"] success:^(BOOL isOk) {
        [MBProgressHUD mj_hideHUD];
        [MBProgressHUD zr_showError:@"邀请成功"];
        [NSObject executeRunloop:^{
            
            [self requestClassId:self.classId scheduleId:self.secheduId];
        } afterDelay:1];
        
        
    } fail:^(NSError *error) {
         [MBProgressHUD mj_hideHUD];
        [MBProgressHUD zr_showError:error.domain];
    }];
    
}



#pragma mark -
//头部
- (SYBackTopView *)backView {
    if (!_backView) {
        _backView = [[SYBackTopView alloc] initWithTitleName:@"缺课学生列表" baseView:self];
    }
    return _backView;
}
- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] init];
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.tableFooterView = [UIView new];
        
    }
    return _curTableView;
}

#pragma mark -
- (SYTopLabView *)topLabView {
    if (!_topLabView) {
        NSArray *xiaoArr = nil;
        CGFloat bili;
        
        xiaoArr = @[@"课程",@"课时",@"上课时间",@"学生",@"学号",@"是否邀请",@"邀请时间",@"邀请"];
        bili = 0.125;
        
        
        _topLabView = [[SYTopLabView alloc] initWithDataNameArr:xiaoArr width:bili];
    }
    return _topLabView;
}



@end
